<template>
    <section class="Coupon">
        <div class="service-items">
            <table>
                <tr>
                    <th width="15%">序号</th>
                    <th width="45%">卡券名称</th>
                    <th>单位</th>
                    <th>单价</th>
                </tr>
                <tr v-for="(v, k) in couponList" :key="k">
                    <td>{{ v.id }}</td>
                    <td>{{ v.couponName }}</td>
                    <td>{{ v.unit + '张' }}</td>
                    <td>{{ v.price + "元/次" }}</td>
                </tr>
            </table>
        </div>
        <div class="busSubmit">
            <button @click="receiveSubmit">
                一键领取优惠券<img src="../images/yes.png" alt="" />
            </button>
        </div>
    </section>
</template>

<script>
    import Request from "@/api/baseRequest.js";
    import { mixin_Coupon } from "@/mixins/coupon.js";
    export default {
        name: "Coupon",
        props: {},
        mixins: [mixin_Coupon],
        data() {
            return {};
        },
        mounted() {
            this.$nextTick(() => {
                this.handleCoupon();
            });
        },
        methods: {
            async handleCoupon() {
                let params = {
                    activityId: this.GetParam(window.location.href, "id") || sessionStorage['id']
                };
                //根据后端所需传参数
                let { code, couponList } = await Request.listCoupon(params);
                if ([200, "200", "OK", 0].includes(code)) {
                    this.couponList = couponList;
                }
            },
            async receiveSubmit() {
                let params = {
                    activityId: this.GetParam(window.location.href, "id") || sessionStorage['id'],
                    userId: this.userId || this.GetParam(window.location.href, "userId") || sessionStorage['userId']
                }
                const { code, msg } = await Request.takeCard(params);
                if ([200, "200", "OK", 0].includes(code)) {
                    this.$Alert({ title: '信息提示', message: msg });
                    setTimeout(() => {
                        this.$router.push({
                            path: '/card',
                            query:{
                                id: params.activityId,
                                userId: params.userId
                            }
                        })
                    }, 1500);
                }
            },
        }
    };
</script>

<style lang="less" scoped>
    @import url("@/styles/coupon.less");
</style>
